package com.yoda.mall.admin.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.yoda.common.Status;
import com.yoda.mall.autoconfig.properties.OssProperties;
import com.yoda.mall.autoconfig.template.OssTemplate;
import com.yoda.mall.model.vo.OssVo;
import com.yoda.mall.model.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 图片上传
 * @author wangjia
 * @date 2022/12/18
 * @Description
 */
@RestController
@RequestMapping("/aliyun")
public class OssController {

    @Autowired
    private OssTemplate ossTemplate;

    @Resource
    private OSS ossClient;

    @Resource
    private OssProperties ossProperties;

    @PostMapping("/oss/upload")
    public R upload(MultipartFile photoFile) throws IOException {
        String uploadPath = ossTemplate.upload(photoFile.getOriginalFilename(), photoFile.getInputStream());
        OssVo ossVo = new OssVo();
        ossVo.setUrl(uploadPath);
        return R.success(ossVo);
        }

    @GetMapping("/oss/policy")
    public R policy() {
        String host = ossProperties.getUrl();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String dir = format + "/"; // 用户上传文件时指定的前缀。
        Map<String, String> respMap = null;
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
            // 创建ossClient实例。

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap = new LinkedHashMap<>();
            respMap.put("accessKeyId", ossProperties.getAccessKeyId());
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));

        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            e.printStackTrace();
        }

        return R.success(respMap);
    }
}
